package activity_disciplinas;

import util.TableHeaderTextView;
import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.Menu;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.Toast;

import com.adjt.droidsapiens.R;

import database.DatabaseConnector;

public class NovaDisciplina extends Activity {

	DatabaseConnector db;
	String tables;
	
	TableRow tr;
	TableLayout layout;
	
	ArrayAdapter<CharSequence> adpDiaSemana;
	ArrayAdapter<CharSequence> adpHorarios;
	
	Intent it;
	int semestreAtual;
	
	EditText nome;
	EditText prof;
	Spinner credito;
	Spinner conceito;
	Spinner[] diaSem = new Spinner[3];
	Spinner[] horaIni = new Spinner[3];
	Spinner[] horaTer = new Spinner[3];
	EditText[] local = new EditText[3];
	int aulaCounter = 0;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_nova_disciplina);
		
		semestreAtual = getIntent().getIntExtra("semestreAtual", 0);
		
		nome = (EditText) findViewById(R.id.NovaDisciplina_nome);
		prof = (EditText) findViewById(R.id.NovaDisciplina_professor);
		credito = (Spinner) findViewById(R.id.NovaDisciplina_creditos);
		conceito = (Spinner) findViewById(R.id.NovaDisciplina_conceito);
		
		Resources r = getResources();
		int px = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 75, r.getDisplayMetrics());
		
		layout = (TableLayout) findViewById(R.id.NovaDisciplina_horariosTableLayout);
		
		TableRow header = new TableRow(this);
		header.setGravity(Gravity.CENTER_HORIZONTAL);
		
		TableHeaderTextView labelDiaSemana = new TableHeaderTextView(this);
		labelDiaSemana.setText(R.string.dia);
		labelDiaSemana.setWidth(px);
		header.addView(labelDiaSemana);
		
		TableHeaderTextView labelHoraInicio = new TableHeaderTextView(this);
		labelHoraInicio.setText(R.string.horarioInicio);
		labelHoraInicio.setWidth(px);
		header.addView(labelHoraInicio);
		
		TableHeaderTextView labelHoraTermino = new TableHeaderTextView(this);
		labelHoraTermino.setText(R.string.horarioTermino);
		labelHoraTermino.setWidth(px);
		header.addView(labelHoraTermino);
		
		TableHeaderTextView labelLocal = new TableHeaderTextView(this);
		labelLocal.setText(R.string.local);
		labelLocal.setWidth(px);
		header.addView(labelLocal);
		
		
		layout.addView(header);
		
		adpDiaSemana = ArrayAdapter.createFromResource(this, R.array.DiaSemana, android.R.layout.simple_spinner_item);
		adpDiaSemana.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		
		adpHorarios = ArrayAdapter.createFromResource(this, R.array.HorariosAula, android.R.layout.simple_spinner_item);
		adpHorarios.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		
		addHorario(new View(this));
		
		db = new DatabaseConnector(getBaseContext());
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.nova_disciplina, menu);
		return true;
	}
	
	public void addDisciplina(View v){
		if(nome.getText().length() > 0){
			 db.open();	 
			 tables = "Disciplina";
			 ContentValues valores = new ContentValues();
			 valores.put("nome", nome.getText().toString());
			 valores.put("nomeProfessor", prof.getText().toString());
			 valores.put("creditos", Integer.parseInt(credito.getSelectedItem().toString()));
			 valores.put("idSemestre", semestreAtual);
			 valores.put("conceito", conceito.getSelectedItem().toString());
			 long idDisciplina = db.inserir(tables, valores);
			 tables = "Aula";
			 for(int i = 0; i < aulaCounter; i++) {
				 valores = new ContentValues();
				 valores.put("diaDaSemana", diaSem[i].getSelectedItem().toString());
				 valores.put("horarioInicio", horaIni[i].getSelectedItem().toString());
				 valores.put("horarioFim", horaTer[i].getSelectedItem().toString());
				 valores.put("local", local[i].getText().toString());
				 valores.put("idDisciplina", idDisciplina);
				 db.inserir(tables, valores);
			 }
			 db.close();
			 Toast.makeText(getBaseContext(),R.string.Toast_disciplinaInseridaSucesso, Toast.LENGTH_SHORT).show();
			 it = new Intent(getBaseContext(), Disciplinas.class);
			 it.putExtra("semestreAtual", this.semestreAtual);
			 startActivity(it);
			 finish();
		}else Toast.makeText(getBaseContext(),R.string.Toast_campoNomeObrigatorio, Toast.LENGTH_SHORT).show();
	}

	public void addHorario(View v){
		if(aulaCounter < 3){
			tr = new TableRow(this);
			tr.setGravity(Gravity.CENTER_HORIZONTAL);
			
			diaSem[aulaCounter] = new Spinner(this);
			diaSem[aulaCounter].setAdapter(adpDiaSemana);
			tr.addView(diaSem[aulaCounter]);
			
			horaIni[aulaCounter] = new Spinner(this);
			horaIni[aulaCounter].setAdapter(adpHorarios);
			tr.addView(horaIni[aulaCounter]);
			
			horaTer[aulaCounter] = new Spinner(this);
			horaTer[aulaCounter].setAdapter(adpHorarios);
			tr.addView(horaTer[aulaCounter]);
			
			local[aulaCounter] = new EditText(this);
			tr.addView(local[aulaCounter]);
			
			aulaCounter++;
			
			layout.addView(tr);
		}
		else Toast.makeText(getBaseContext(),R.string.Toast_addHorario, Toast.LENGTH_SHORT).show();
	}
	
	public void removeHorario(View v) {
		if(aulaCounter > 1) {
			layout.removeViewAt(aulaCounter);
			aulaCounter--;
		}
		else Toast.makeText(getBaseContext(),R.string.Toast_removeHorario, Toast.LENGTH_SHORT).show();
	}
	
	@Override
	public void onBackPressed(){
		it = new Intent(getBaseContext(), Disciplinas.class);
		it.putExtra("semestreAtual", semestreAtual);
		startActivity(it);
		finish();
	}
}
